package com.integral.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.integral.biz.IBankBiz;
import com.integral.entity.Bank;
import com.integral.entity.UserInfo;
import com.integral.util.UploadImgUtil;
import com.integral.vo.BankVo;

@Controller
@RequestMapping("/bank")
public class BankController {

	protected final Log logger = LogFactory.getLog(getClass());
	@Resource
	private IBankBiz bankBiz;
	
	/**
	 * 查询银行信息列表
	 * @param request
	 * @param response
	 * @param bankVo
	 * @return
	 */
	@RequestMapping(value="/findBank")
	@ResponseBody
	public Map<String,Object> findBank(HttpServletRequest request, HttpServletResponse response,BankVo bankVo ){
		//查询未删除数据
		bankVo.setDeleteFlag(0);
		List<BankVo> list = bankBiz.findBank(bankVo);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", bankBiz.findBankCount(bankVo));
		map.put("rows", list);
		return map;
	}
	/**
	 * 新增银行信息
	 * @param bankVo
	 * @return
	 */
	@RequestMapping(value="/addBank")
	public String addBank(HttpServletRequest request,BankVo bankVo){
		Bank bank = new Bank();
		bank.setBankCode(bankVo.getBankCode());
		bank.setBankExchange(bankVo.getBankExchange());
		bank.setBankName(bankVo.getBankName());
		bank.setInceptionUnit(bankVo.getInceptionUnit());
		//上传文件
		String bankImg = UploadImgUtil.dowloadFile(bankVo.getImg(), request);
		//上传文件
		String logoImg = UploadImgUtil.dowloadFile(bankVo.getLogoImg(), request);
		if("false".equals(bankImg)){
			bank.setBankImg(null);
		}else{
			bank.setBankImg(bankImg);
		}
		if("false".equals(logoImg)){
			bank.setBankLogo(null);
		}else{
			bank.setBankLogo(logoImg);
		}
		bankBiz.addBank(bank);
		return "management/bank";
	}
	/**
	 * 修改银行信息
	 * @param bankVo
	 * @return
	 */
	@RequestMapping(value="/updateBank")
	@ResponseBody
	public String updateBank(BankVo bankVo,HttpServletRequest request){
		String msg = null;
		try {
			//获取登录对象
			HttpSession session = request.getSession();
			UserInfo user = (UserInfo)session.getAttribute("userInfo");
			
			Bank bank = new Bank();
			//ID
			bank.setId(bankVo.getId());
			//银行编码
			bank.setBankCode(bankVo.getBankCode());
			//积分兑换信息
			bank.setBankExchange(bankVo.getBankExchange());
			//银行名称
			bank.setBankName(bankVo.getBankName());
			//起兑单位
			bank.setInceptionUnit(bankVo.getInceptionUnit());
			//银行长图
			if(null != bankVo.getImg()){
				String bankImg = UploadImgUtil.dowloadFile(bankVo.getImg(), request);
				if("false".equals(bankImg)){
					bank.setBankImg(null);
				}else{
					bank.setBankImg(bankImg);
				}
				
			}
			//银行图标
			if(null != bankVo.getLogoImg()){
				String bankLogoImg = UploadImgUtil.dowloadFile(bankVo.getLogoImg(), request);
				if("false".equals(bankLogoImg)){
					bank.setBankLogo(null);
				}else{
					bank.setBankLogo(bankLogoImg);
				}
			}
			//设置修改人名
			bank.setUpdateEmp(user.getUserName());
			bank.setUpdateTime(new Date());
			bankBiz.updateBank(bank);
			msg = "修改成功！";
		} catch (Exception e) {
			msg = "修改失败！";
			logger.error("修改银行信息异常",e);
		}
		return msg;
	}
	/**
	 * 删除银行信息
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delBank")
	@ResponseBody
	public String delBank(Integer id,HttpServletRequest request){
		String msg = null;
		try {
			//获取登录对象
			HttpSession session = request.getSession();
			UserInfo user = (UserInfo)session.getAttribute("userInfo");
			Bank bank = new Bank();
			bank.setId(id);
			bank.setDeleteFlag(1);
			//设置修改人名
			bank.setUpdateEmp(user.getUserName());
			bank.setUpdateTime(new Date());
			bankBiz.updateBank(bank);
			msg = "删除成功！";
		} catch (Exception e) {
			msg = "删除失败！";
			logger.error("删除银行信息异常",e);
		}
		return msg;
	}
}
